"""
domU Creator
Author: Manula Waidyanatha
This is a Xen tools based application to create paravirtualized domains

"""

import sys
import os
import gtk.glade

class main:
    wTree = None
    
    def __init__(self):
        self.wTree = gtk.glade.XML("Main.glade")
        signals = {
                   "on_Main_destroy":self.quit,
                   "on_ok_btn_clicked":self.create_conf,
                   "on_cancel_btn_clicked": self.quit,
                   }
        self.wTree.signal_autoconnect(signals)
        
        # Set default vaules in filesystem combo box (ext3)
        self.wTree.get_widget("fs").set_active(0)
        
        # Set default value in distribution combo box (lenny)
        self.wTree.get_widget("dist").set_active(3)
        
        # Set items to kernel and initrd combo boxes
        self.set_combo("kernel")
        self.set_combo("initrd")
        self.wTree.get_widget("kernel").set_active(0)
        self.wTree.get_widget("initrd").set_active(0)
        
        gtk.main()
    
    def quit(self, widget):
        sys.exit(0)
        
    def create_conf(self, widget):
        vmname = self.wTree.get_widget("name").get_text()
        directory = self.wTree.get_widget("dir").get_text()
        
        size = self.wTree.get_widget("size").get_text()        
        if self.wTree.get_widget("size_rdo_btn_mb").state:
            size = size+"Mb"
        else:
            size = size+"Gb"
            
        memory = self.wTree.get_widget("memory").get_text()
        if self.wTree.get_widget("mem_rdo_btn_mb").state:
            memory = memory+"Mb"
        else:
            memory = memory+"Gb"
        
        swap = self.wTree.get_widget("swap").get_text()
                
        filesystem = self.wTree.get_widget("fs").get_active_text()
        distribution = self.wTree.get_widget("dist").get_active_text()
        ip = self.wTree.get_widget("ip").get_text()
        kernel = self.wTree.get_widget("kernel").get_active_text()
        initrd = self.wTree.get_widget("initrd").get_active_text()
        
        #for attr in dir(self.wTree.get_widget("swap_rdo_btn_mb")):
            #print attr
        #if self.wTree.get_widget("size_rdo_btn_mb").state:
         #   print 'true'
        #if self.wTree.get_widget("mem_rdo_btn_mb").state:
        #    print 'true'
        #if self.wTree.get_widget("swap_rdo_btn_mb").state:
         #   print 'true'
                          
        conf_file = ("dir\t= " + directory + "\nsize\t= " + size+
                     "\nmemory\t= "+memory)
        if swap:
            if self.wTree.get_widget("swap_rdo_btn_mb").state:
                swap = swap+"Mb"
            else:
                swap = swap+"Gb"
            conf_file +="\nswap\t= "+swap
        else:
            conf_file += "\nnoswap\t= 1"
            
        conf_file = (conf_file+"\nfs\t= "+filesystem+"\ndist\t= "+distribution+
        "\nkernel\t= "+kernel+"\ninitrd\t= "+initrd)
        FILE = open("xen-tools.sxp", "w")
        FILE.writelines(conf_file)
        FILE.close
        print "xen-create-image --hostname "+vmname
        print conf_file
        
        
    def set_combo(self, item):
        combo = self.wTree.get_widget(item)
        
        if item == 'kernel':
            item = 'vmlinuz'
        
        dir_list= os.listdir("/boot")
        list = []
        
        for file in dir_list:
            if item in file:
                if 'xen' in file:
                    print file, item
                    list.append(file)
        
        for list_item in list:      
            combo.append_text(list_item)
        
        
start = main()
